package red.ouo.distributedtools.redis.message.processor;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import red.ouo.distributedtools.standard.ProxyHeader;
import red.ouo.distributedtools.redis.message.heander.RedisRegisterQueueHeader;
import red.ouo.distributedtools.utils.ReflectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

@Aspect
@Component
public class RegisterQueueProcessor {
    @Around(value = "execution(* red.ouo.distributedtools.redis.message.config.RedisMessageConfig.registerQueue(..))&& args(queueName,executorService)&& target(bean), argNames = pjp,queueName,executorService,bean", argNames = "pjp,queueName,executorService,bean")
    public Object registerQueueAround(ProceedingJoinPoint pjp, String queueName, ExecutorService executorService, Object bean) {
        Map map = ReflectUtils.getAllInterfaceAchieve(RedisRegisterQueueHeader.class);
        List<Object> end = null;
        try {
            if(map==null||map.size()==0) return pjp.proceed();
            end = ReflectUtils.<String>invokeMethod(new ArrayList<>(map.values()), "header", new Class[]{String.class, ExecutorService.class, ProxyHeader.class}, queueName, executorService, new ProxyHeader(pjp));
            System.out.println(end.getClass());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return end.get(0);
    }
}
